package com.zwy.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.args.ListPosition;
import redis.clients.jedis.resps.KeyedListElement;

@Component
public class RedisListUtil {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private JedisPool jedisPool;

    public long lpush(int dbIndex, String key, String... values){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.lpush(key, values);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public long rpush(int dbIndex, String key, String... values){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.rpush(key, values);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public String rpoplpush(int dbIndex, String key, String value){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.rpoplpush(key, value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public String l(int dbIndex, String source, String destination, int timeout){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.brpoplpush(source, destination, timeout);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public long llen(int dbIndex, String key){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.llen(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public String lindex(int dbIndex, String key, long index){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.lindex(key, index);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public long linsert(int dbIndex, String key, ListPosition where, String pivot, String value){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.linsert(key, where, pivot, value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public boolean lset(int dbIndex, String key, long index, String value){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return "OK".equalsIgnoreCase(jedis.lset(key, index, value));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public boolean ltrim(int dbIndex, String key, long start, long stop){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return "OK".equalsIgnoreCase(jedis.ltrim(key, start, stop));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public long ltrim(int dbIndex, String key, long count, String value){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.lrem(key, count, value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public String lpop(int dbIndex, String key){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.lpop(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public String rpop(int dbIndex, String key){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.rpop(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public KeyedListElement blpop(int dbIndex, long timeout, String... keys){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.blpop(timeout, keys);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public KeyedListElement brpop(int dbIndex, long timeout, String... keys){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.brpop(timeout, keys);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }
}
